/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.newmodel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 
 * @author i_khri
 */
public class Index {
	/** Class Constructor */
	public Index() {
		entries = new HashMap<Term, PostingsList>();
	}

	/**
	 * @param tmpIndexPath
	 *            the temporary index path to save information to
	 */
	public void saveTo(String tmpIndexPath) {
		File tmpIndexPathFile = new File(tmpIndexPath);
		File parentFile = tmpIndexPathFile.getParentFile();

		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}

		try {
			FileOutputStream fos = new FileOutputStream(tmpIndexPath);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

			Map<Term, PostingsList> entriesSorted = sortEntries();
			StringBuffer entriesString = new StringBuffer();

			for (Term t : entriesSorted.keySet()) {
				entriesString
						.append(t + entriesSorted.get(t).toString() + "\n");
			}

			osw.write(entriesString.toString());
			osw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/** @return the sorted entries of the Dictionary */
	private Map<Term, PostingsList> sortEntries() {
		Map<Term, PostingsList> entriesSorted = new TreeMap<Term, PostingsList>();
		entriesSorted.putAll(entries);
		return entriesSorted;
	}

	/**
	 * @param docId
	 *            the document id we are working with currently
	 * @param term
	 *            the term we are currently working with
	 */
	public void addRecord(Term term, int docId) {
		if (entries.containsKey(term)) {
			PostingsList pl = entries.get(term);
			pl.addDocId(docId);
		} else {
			PostingsList pl = new PostingsList();
			pl.addDocId(docId);
			entries.put(term, pl);
		}
	}

	/**
	 * @param term
	 *            the term to add to the posting list
	 * @param postList
	 *            the posting list to append to
	 */
	public void addPostingsListForTerm(Term term, PostingsList postList) {
		entries.put(term, postList);
	}

	/**
	 * @param theString
	 *            the string which the term is extracted from
	 * @return the term that extracted from the string
	 */
	public Term addTermPostingsListFromIndexString(String theString) {
		Term term = Term.fromIndexString(theString);
		entries.put(term, PostingsList.fromIndexString(theString));
		return term;
	}

	/**
	 * @param term
	 *            the term which we're interested in the postings list
	 * @return the posting list which contained the term
	 */
	public PostingsList getPostingsList(Term term) {
		return entries.get(term);
	}

	/**
	 * 
	 * @return the key set for iterations...
	 */
	public Set<Term> keySet() {
		return entries.keySet();
	}

	/**
	 * 
	 * @return true or false is the entries are empty
	 */
	public boolean isEmpty() {
		return entries.isEmpty();
	}

	/**
	 * 
	 * @param term
	 *            check if the term is inside the set
	 * @return true or false if the term exists or not respectively
	 */
	public boolean containsTerm(Term term) {
		return entries.containsKey(term);
	}

	/**
	 * Clear the entries
	 */
	public void clear() {
		entries.clear();
	}

	/** The private Map which will contain the terms and postings lists */
	private Map<Term, PostingsList> entries;

}
